<!--
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-02-20 15:00:48
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-08-27 11:29:23
 * @FilePath: /XMate-h5/src/views/myXmate/myTeam/teamVisit/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="h-full bg-white">
    <listHead
      @search="onSearch"
      :enableDrpt="true"
      :drptTitle="cp_drptTitle"
      @filter="onClickFilter"
      @drptFilter="onClickDrptFilter"
      :defaultDateRange="[visitLogic.startTime, visitLogic.endTime]"
    />

    <visitList
      :list="visitListData"
      :loading="visitLogic.loading"
      :finished="noMore"
      :refreshing="visitLogic.refreshing"
      :onRefresh="onRefreshVisit"
      :loadMoreData="loadMoreData"
      :myTeam="true"
    />
    <teamVisitorFilter
      ref="teamVisitorFilterRef"
      @onConfirm="onConfirmFilter"
      :default-date-range="[visitLogic.startTime, visitLogic.endTime]"
    />

    <chooseTeamView
      ref="chooseTeamViewRef"
      :selectItems="selectTeams"
      @onConfirm="onConfirmTeam"
    />
  </div>
</template>

<script setup>
import visitList from "@/views/myXmate/components/visitList.vue";
import teamVisitorFilter from "@/views/myXmate/components/teamVisitorFilter.vue";
import listHead from "@/views/myXmate/components/listHead.vue";
import chooseTeamView from "@/views/myXmate/components/chooseTeamView.vue";
import { ref, computed, onMounted } from "vue";
import { useMyXmateStore } from "@/store";
import { getDateRange, formatCompletionTime, ParameterDateType } from "@/utils/timeTool";

const store = useMyXmateStore();

const teamVisitorFilterRef = ref(null);
const chooseTeamViewRef = ref(null);
const visitLogic = ref({
  list: [],
  loading: false,
  refreshing: false,
  startTime: getDateRange("inPastMonth").startDate,
  endTime: getDateRange("inPastMonth").endDate,
  stageIds: [],
  page: 1,
  dptIds: [],
  userIds: [],
});
function onClickFilter() {
  console.log("onClickFilter", teamVisitorFilterRef.value);
  teamVisitorFilterRef.value.show();
}

const selectTeams = ref([]);

const cp_drptTitle = computed(() => {
  if (selectTeams.value.length == 0) {
    return "全部部门";
  }
  return selectTeams.value.map((v) => v.name).join(",");
});
function onClickDrptFilter() {
  chooseTeamViewRef.value.show();
}

function onConfirmTeam(teams) {
  const needRefresh = selectTeams.value !== teams;
  console.log("onConfirmTeam: ", teams);
  selectTeams.value = teams;
  visitLogic.value.dptIds = teams.map((v) => v.id);
  if (needRefresh) {
    getData(makeAPIParameter());
  }
}

function makeAPIParameter() {
  return {
    searchKey: searchText.value,
    startTime: formatCompletionTime(
      visitLogic.value.startTime,
      ParameterDateType.startOfDay
    ),
    endTime: formatCompletionTime(visitLogic.value.endTime, ParameterDateType.endOfDay),
    dptIds: visitLogic.value.dptIds,
    pageNumber: visitLogic.value.page,
    pageSize: 20,
    asc: false,
    syncStatus: visitLogic.value.syncStatus,
    reportStatus: visitLogic.value.reportStatus,
    userIds: visitLogic.value.userIds,
    stageIds: visitLogic.value.stageIds,
    targetStatus: visitLogic.value.targetStatus,
    abilityStatus: visitLogic.value.abilityStatus,
    taskStatus: visitLogic.value.taskStatus,
  };
}

function onConfirmFilter(parameter) {
  const { dateRange, stageIds } = parameter;
  delete parameter.dateRange;
  visitLogic.value = parameter;
  visitLogic.value.page = 1;
  visitLogic.value.stageIds = stageIds;
  if (dateRange) {
    visitLogic.value.startTime = dateRange[0];
    visitLogic.value.endTime = dateRange[1];
  }
  getData(makeAPIParameter());
}

const visitListData = computed(() => store.teamVisitorList);
const noMore = computed(() => store.teamVisitorNoMoreData);

const searchText = ref("");

onMounted(() => {
  getData(makeAPIParameter());
  store.getMydprtAction();
});

function onSearch(v) {
  visitLogic.value.page = 1;
  searchText.value = v;
  getData(makeAPIParameter());
}

async function onRefreshVisit() {
  visitLogic.value.refreshing = true;
  visitLogic.value.page = 1;
  await getData(makeAPIParameter());
  visitLogic.value.refreshing = false;
}

async function loadMoreData() {
  visitLogic.value.page++;
  await getData(makeAPIParameter());
}

async function getData() {
  visitLogic.value.loading = true;
  await store.getTeamVisitListAction(makeAPIParameter());
  visitLogic.value.noMore = true;
  visitLogic.value.loading = false;
}
</script>

<style lang="less" scoped></style>
